'use strict';
const { Forbbiden } = require('@utils/http-exception');

module.exports = options => {
  return async (ctx, next) => {
    const { referer } = ctx.request.header;
    if (referer) {
      const url = new URL(referer);
      if (options.includes(url.host)) {
        await next();
      } else {
        throw new Forbbiden(`${url.host} 禁止访问`);
      }
    } else {
      await next();
    }
  };
};
